/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.SalesmanSessionBean;
import crm.clientManagement.entity.CompanyEntity;
import java.io.IOException;
import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import crm.salesOrderProcessing.entity.LineItemEntity;
import crm.salesOrderProcessing.entity.SalesOrderEntity;
import crm.salesOrderProcessing.session.SalesOrderSessionBean;
import crm.salesOrderProcessing.util.exception.SalesOrderStatusMismatchException;
import crm.salesOrderProcessing.util.exception.SalesmanAmountLimitExceededException;
import crm.salesOrderProcessing.util.exception.SalesmanNotExistException;

/**
 *
 * @author ReinForceII
 */
@ManagedBean
@ViewScoped
public class ConfirmSalesOrderManagedBean {

    @EJB
    SalesOrderSessionBean salesOrderSessionBean;
    private SalesmanEntity salesman;
    private Long salesOrderId;
    private SalesOrderEntity salesOrder;
    @EJB
    SalesmanSessionBean salesmanManager;
    private CompanyEntity customer;
    private Collection<LineItemEntity> lineItemList;
    private String errorMessage;
    
    /** Creates a new instance of ConfirmSalesOrderManagedBean */
    public ConfirmSalesOrderManagedBean() {
        
        salesOrder = (SalesOrderEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("purchaseOrder");
        lineItemList = salesOrder.getSalesOrderLineItems();
        errorMessage = "";
    }
 
//    public void reDirect() throws IOException{
//        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("lastURL", "confirmeSalesOrder");
//        FacesContext.getCurrentInstance().getExternalContext().redirect("salesOrderactionResult.xhtml");
//    }
    
    private SalesmanEntity getCurrentUser(){
    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    Long salesmanId =  (Long)request.getSession().getAttribute("userId");
    return salesmanManager.getSalesmanById(salesmanId);
    }
    
    public SalesOrderEntity getSalesOrder() {
        return salesOrder;
    }

    public void setSalesOrder(SalesOrderEntity salesOrder) {
        this.salesOrder = salesOrder;
    }

    public CompanyEntity getCustomer() {
        return customer;
    }

    public void setCustomer(CompanyEntity customer) {
        this.customer = customer;
    }

    public Collection<LineItemEntity> getLineItemList() {
        return lineItemList;
    }

    public void setLineItemList(Collection<LineItemEntity> lineItemList) {
        this.lineItemList = lineItemList;
    }

    public void confirmSalesOrder(ActionEvent event) throws IOException{
        
        salesman =  getCurrentUser();
         
        try {
            salesOrderSessionBean.confirmSalesOrder(salesman, salesOrder);

        } catch (SalesmanAmountLimitExceededException e) {
           setErrorMessage("No enough authority to approve");
           return;
        } catch (SalesmanNotExistException e){
           setErrorMessage("Salesman not exist");
           return;
        } catch (SalesOrderStatusMismatchException e){
           setErrorMessage("Sales order cannot be confirmed, " + e.getMessage());
           return;
        }
         
         FacesContext.getCurrentInstance().getExternalContext().getFlash().put("purchaseOrder", salesOrder);
         FacesContext.getCurrentInstance().getExternalContext().getFlash().put("lastURL", "confirmeSalesOrder");
         FacesContext.getCurrentInstance().getExternalContext().redirect("salesOrderactionResult.xhtml");
    }

    public SalesmanEntity getSalesman() {
        return salesman;
    }

    public void setSalesman(SalesmanEntity salesman) {
        this.salesman = salesman;
    }

    /**
     * @return the errorMessage
     */
    public String getErrorMessage() {
        return errorMessage;
    }

    /**
     * @param errorMessage the errorMessage to set
     */
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }


}
